<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="模板编码" prop="templateNumber">
        <el-input
          v-model="queryParams.templateNumber"
          placeholder="请输入模板编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模板名称" prop="templateName">
        <el-input
          v-model="queryParams.templateName"
          placeholder="请输入模板名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模板状态" prop="templateStatus">
        <el-select v-model="queryParams.templateStatus" placeholder="请选择模板状态" clearable>
          <el-option
            v-for="dict in dict.type.yh_template_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="数据库表名" prop="databankName">
        <el-input
          v-model="queryParams.databankName"
          placeholder="请输入数据库表名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数据库表编码" prop="databankNumber">
        <el-input
          v-model="queryParams.databankNumber"
          placeholder="请输入数据库表编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <!--<el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dctemplateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="id" align="center" prop="id"/>
      <el-table-column label="模板编码" align="center" prop="templateNumber"/>
      <el-table-column label="模板名称" align="center" prop="templateName"/>
      <el-table-column label="模板状态" align="center" prop="templateStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yh_template_status" :value="scope.row.templateStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="数据库表名" align="center" prop="databankName"/>
      <el-table-column label="数据库表编码" align="center" prop="databankNumber"/>
      <el-table-column label="模板分组" align="center" prop="note1"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="detail(scope.row)"
          >查看详细
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改导出模板对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="模板编码" prop="templateNumber">
          <el-input v-model="form.templateNumber" placeholder="请输入模板编码"/>
        </el-form-item>
        <el-form-item label="模板名称" prop="templateName">
          <el-input v-model="form.templateName" placeholder="请输入模板名称"/>
        </el-form-item>
        <el-form-item label="模板状态" prop="templateStatus">
          <el-radio-group v-model="form.templateStatus">
            <el-radio
              v-for="dict in dict.type.yh_template_status"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="数据库表名" prop="databankName">
          <el-input v-model="form.databankName" placeholder="请输入数据库表名"/>
        </el-form-item>
        <el-form-item label="数据库表编码" prop="databankNumber">
          <el-input v-model="form.databankNumber" placeholder="请输入数据库表编码"/>
        </el-form-item>
        <el-form-item label="模板分组" prop="note1">
          <el-select v-model="form.note1" placeholder="请选择上报事件类型" clearable>
            <el-option
              v-for="dict in dict.type.he_report_event_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 弹出框 -->
    <el-dialog :visible.sync="dialogVisible" title="字段信息" width="50%">
      <!-- 使用表格布局 -->
      <el-table :data="fieldList" border>
        <!-- 字段名称 -->
        <el-table-column label="字段名称">
          <template slot-scope="scope">
            <span>{{ scope.row.fieldName }}</span>
          </template>
        </el-table-column>
        <!-- 字段编码 -->
        <el-table-column label="字段编码">
          <template slot-scope="scope">
            <span>{{ scope.row.fieldNumber }}</span>
          </template>
        </el-table-column>
        <!-- 字段类型 -->
        <el-table-column label="字段类型">
          <template slot-scope="scope">
            <span>{{ scope.row.fieldKind }}</span>
          </template>
        </el-table-column>
        <!-- 字段顺序 -->
        <el-table-column label="字段顺序">
          <template slot-scope="scope">
            <!-- 可编辑的输入框 -->
            <el-input v-model="scope.row.fieldSeq" :placeholder="'请输入字段顺序'"/>
          </template>
        </el-table-column>
        <!-- 导出状态 -->
        <el-table-column label="导出状态">
          <template slot-scope="scope">
            <!-- 使用开关组件 -->
            <el-switch v-model="scope.row.fieldDeriveStatus" active-color="#13ce66" inactive-color="#ff4949"
                       active-text="开启" inactive-text="关闭"/>
          </template>
        </el-table-column>
        <!-- 开关状态 -->
        <el-table-column label="开关状态">
          <template slot-scope="scope">
            <!-- 使用开关组件 -->
            <el-switch v-model="scope.row.fieldSwitchStatus" active-color="#13ce66" inactive-color="#ff4949"
                       active-text="开启" inactive-text="关闭"/>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">确 定</el-button>
        <!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDctemplate,
  getDctemplate,
  delDctemplate,
  addDctemplate,
  updateDctemplate
} from "@/api/module/ny/dctemplate";
import {SearchField, updateAllField} from "@/api/module/ny/dcfield";
import {Message} from "element-ui";

export default {
  name: "Dctemplate",
  dicts: ['yh_template_status','he_report_event_type'],
  data() {
    return {
      dialogVisible: false,

      //存放表的所有字段信息
      fieldList: [],

      databankName: null,

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 导出模板表格数据
      dctemplateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        templateNumber: null,
        templateName: null,
        templateStatus: null,
        databankName: null,
        databankNumber: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询导出模板列表 */
    getList() {
      this.loading = true;
      listDctemplate(this.queryParams).then(response => {
        this.dctemplateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    detail(row) {

      //将这个外键传入到后台
      this.databankName = row.databankName

      SearchField(this.databankName,row.templateNumber).then(response => {
        this.fieldList = response;
        this.fieldList[0].templateId = row.id
        this.fieldList[0].templateNumber = row.templateNumber
        this.fieldList[0].templateName = row.templateName
        this.fieldList[0].templateStatus = row.templateStatus
        this.fieldList[0].databankName = row.databankName
        this.fieldList[0].databankNumber = row.databankNumber

        this.dialogVisible = true
        // this.ziduanopen=true;
        console.log(response)
      });
    },
    submit() {
      updateAllField(this.fieldList).then(response => {
        if (response == 1) {
          Message.success("数据添加成功！");
          this.dialogVisible = false
        }
        if (response == 2) {
          Message.success("数据修改成功！");
          this.dialogVisible = false
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.dialogVisible = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        templateNumber: null,
        templateName: null,
        templateStatus: null,
        databankName: null,
        databankNumber: null,
        note1: null,
        note2: null,
        note3: null,
        note4: null,
        note5: null,
        note6: null,
        note7: null,
        note8: null,
        note9: null,
        note10: null,
        userid: null,
        deptid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加导出模板";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDctemplate(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改导出模板";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDctemplate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDctemplate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除导出模板编号为"' + ids + '"的数据项？').then(function () {
        return delDctemplate(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/dctemplate/export', {
        ...this.queryParams
      }, `dctemplate_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
